<template>
  <div>
    <!-- 最近更新管理 -->
    <!-- 按钮上面的导航 -->
    <!-- <el-row style="margin: 18px 0px 0px 18px "> -->
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">管理中心</el-breadcrumb-item>
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        <el-breadcrumb-item>最近更新管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>

    <!-- 搜索与添加按钮 -->
    <div style="margin-top: 15px; margin-left: 12px;">

      <el-input
        style="width: 250px;float:left;"
        placeholder="请输入内容"
        suffix-icon="el-icon-date"
        v-model="queryKey1"
        clearable
      ></el-input>

    <el-button style="float:left;" type="primary" @click="loadNews()" icon="el-icon-search">搜索</el-button>
    <el-button style="float:left;" type="primary" icon="el-icon-refresh" @click="loadNews1()" >刷新</el-button>
    </div>
    <news-add @onSubmit="loadNews()" ref="edit"></news-add>

    <el-card style="margin: 15px 1%;width: 98%">
      <el-table :data="fromData" border style="width: 100%" :max-height="tableHeight">
        <el-table-column prop="id" width="55" label="ID"></el-table-column>
        <el-table-column prop="newContent" label="最近更新内容" fit></el-table-column>
         <el-table-column prop="createTime" width="200" label="创建时间"></el-table-column>
        <el-table-column prop="username" width="150" label="最后操作人" fit></el-table-column>

        <el-table-column fixed="right" label="操作"  width="120">
          <template slot-scope="scope">
            <el-button
              @click="editNews(scope.row)"
              type="primary"
              size="small"
              icon="el-icon-edit"
              circle
            ></el-button>
            <el-button
              @click="deleteNews(scope.row.id)"
              type="danger"
              size="small"
              icon="el-icon-delete"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 20px 0 50px 0">
        <el-pagination
          background
          style="float:right;"
          layout="total, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import NewsAdd from './newsAdd.vue'
export default {
  name: 'news',
  components: { NewsAdd },
  data () {
    return {
      fromData: [],
      queryKey1: '',
      pageSize: 10,
      total: 0
    }
  },
  mounted () {
    this.loadNews()
  },
  computed: {
    tableHeight () {
      // table  card的高度
      return window.innerHeight - 300
    }
  },
  methods: {
    loadNews1 () {
       // 刷新清空搜索框的值
      // eslint-disable-next-line no-unused-expressions
      this.queryKey1 = ''
      this.loadNews()
    },
    loadNews () {
      var _this = this
      this.$axios.post('/admin/news/' + this.pageSize + '/1?queryKey1=' + this.queryKey1).then((resp) => {
        if (resp && resp.data.code === 200) {
          _this.fromData = resp.data.result.content
          _this.total = resp.data.result.totalElements
           this.$message({
              type: 'success',
              message: '刷新成功'
            })
        } else {
          // 从后端返回错误信息
            this.$alert('<p style=" color:#F00;">' + resp.data.message + '</p>', '错误提示', {
              dangerouslyUseHTMLString: true
            })
        }
      })
    },
    handleCurrentChange (page) {
      var _this = this
      this.$axios.post('/admin/news/' + this.pageSize + '/' + page + '/').then((resp) => {
        if (resp && resp.data.code === 200) {
          _this.fromData = resp.data.result.content
          _this.total = resp.data.result.totalElements
        } else {
              // 从后端返回错误信息
              this.$alert(resp.data.message, '错误提示', {
              type: 'error',
              dangerouslyUseHTMLString: true
              })
        }
      })
    },
    editNews (item) {
      this.$refs.edit.dialogFormVisible = true
      this.$refs.edit.form = {
        id: item.id,
        username: item.username,
        newContent: item.newContent,
        createTime: item.createTime
      }
    },
    deleteNews (id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.delete('/admin/news/' + id).then((resp) => {
            if (resp && resp.data.code === 200) {
              this.$message({
                message: resp.data.message,
                type: 'success'
              })
              this.loadNews()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped>
.add-link {
  margin: 18px 0 15px 10px;
  float: left;
}
</style>
